package com.boxing;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "LoginServlet", value = "/LoginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        String name = request.getParameter("name");
        String psd = request.getParameter("psd");
        System.out.println("用户名: "+ name+"  密码:"+psd);

        if ("张三".equals(name)){
            // 登录成功
            // 跳转登录成功的页面
            request.getRequestDispatcher("/succ.html").forward(request, response);
        }else {
            // 登录失败
            // 请求重定向
            response.sendRedirect("/TomcatDemo/loginFail.html");
            // 把响应码设置成 302    设置响应头 Location : /TomcatDemo/loginFail.html
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        System.out.println("执行到了 doPost方法");
        doGet(request, response);
    }
}
